package dao;

import model.Group;
import util.DatabaseUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class GroupDAO {
    public static void addGroup(Group group) {
        String sql = "INSERT INTO `group` (group_name, class_id, leader, member_count, description) VALUES (?, ?, ?, ?, ?)";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            
            pstmt.setString(1, group.getGroupName());
            pstmt.setInt(2, group.getClassId());
            pstmt.setString(3, group.getLeader());
            pstmt.setInt(4, group.getMemberCount());
            pstmt.setString(5, group.getDescription());
            
            pstmt.executeUpdate();
            
            ResultSet rs = pstmt.getGeneratedKeys();
            if (rs.next()) {
                group.setId(rs.getInt(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static List<Group> getAllGroups() {
        List<Group> groups = new ArrayList<>();
        String sql = "SELECT * FROM `group`";
        
        try (Connection conn = DatabaseUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                groups.add(new Group(
                    rs.getInt("id"),
                    rs.getString("group_name"),
                    rs.getInt("class_id"),
                    rs.getString("leader"),
                    rs.getInt("member_count"),
                    rs.getString("description")
                ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return groups;
    }

    public static List<Group> getGroupsByClassId(int classId) {
        List<Group> groups = new ArrayList<>();
        String sql = "SELECT * FROM `group` WHERE class_id = ?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, classId);
            ResultSet rs = pstmt.executeQuery();
            
            while (rs.next()) {
                groups.add(new Group(
                    rs.getInt("id"),
                    rs.getString("group_name"),
                    rs.getInt("class_id"),
                    rs.getString("leader"),
                    rs.getInt("member_count"),
                    rs.getString("description")
                ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return groups;
    }

    public static void updateGroup(Group group) {
        String sql = "UPDATE `group` SET group_name=?, class_id=?, leader=?, member_count=?, description=? WHERE id=?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, group.getGroupName());
            pstmt.setInt(2, group.getClassId());
            pstmt.setString(3, group.getLeader());
            pstmt.setInt(4, group.getMemberCount());
            pstmt.setString(5, group.getDescription());
            pstmt.setInt(6, group.getId());
            
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void deleteGroup(int id) {
        String sql = "DELETE FROM `group` WHERE id=?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, id);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Group getGroupById(int id) {
        String sql = "SELECT * FROM `group` WHERE id=?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            
            if (rs.next()) {
                return new Group(
                    rs.getInt("id"),
                    rs.getString("group_name"),
                    rs.getInt("class_id"),
                    rs.getString("leader"),
                    rs.getInt("member_count"),
                    rs.getString("description")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
//小组数据的操作，如获取所有小组、根据ID获取小组、新增小组、删除小组和更新小组信息。